package com.PAT.UnSatisfying;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2021/11/29/20:33
 * @Description: 差7分
 * @Target: 找出镇上最年长和最年轻的人
 */
public class 人口普查 {

    public static void main(String[] args) {

       // 直接处理进来的数据
        Scanner in = new Scanner(System.in);
        int N = Integer.valueOf(in.next());
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        String Name1 = "";
        String Name2 = "";
        int temp = 0;
        int count = 0;

        while (N>0){
            String S1 = in.next();
            String S2 = in.next();
            temp = GetDaysByYYMMDD(S2);
            if (temp>0){
                count++;

                if (temp>max){
                    max = temp;
                    Name1 = S1;
                }

                if (temp<min){
                    min = temp;
                    Name2 = S1;
                }
            }
            N--;

        }


        System.out.println(count+" "+ Name1 + " "+ Name2);



    }




    // 年后算(如2000年,那么从2001的一月算起),2014年前
    public static int YearDiff(int year){
        int yearDiff = 0;
        if (year>2014){
            for (int i = 2014; i < year; i++) {
                if((i%4==0&&i%100!=0)||i%400==0){
                    yearDiff+=366;
                }else {
                    yearDiff+=365;
                }
            }
            yearDiff = 0-yearDiff;
        }else {
            for (int i = year+1; i <= 2014-1; i++) {
                if((i%4==0&&i%100!=0)||i%400==0){
                    yearDiff+=366;
                }else {
                    yearDiff+=365;
                }
            }
        }

        if (yearDiff>365*200||2014-year>200){
            yearDiff = Integer.MIN_VALUE;
        }
        return yearDiff;
    }

    public static int MonthDiff(int year,int month){
        int monthDiff = 0;
        // 先计算2014年的
        if (year!=2014){
            for (int i = 1; i <= 9; i++) {
                monthDiff+=getMonthDays(2014,i);
            }   // 273天

            for (int i = month+1; i <= 12; i++) {
                monthDiff+=getMonthDays(year,i);
            }
        }else {
            if (month<9){
                for (int i = month; i < 9; i++) {
                    monthDiff+=getMonthDays(year,i);
                }
            }else if (month>9){
                for (int i = 10; i <= month; i++) {
                    monthDiff+=getMonthDays(year,i);
                }
                monthDiff = 0-monthDiff;
            }else if (month==9){
                monthDiff = 0;
            }
        }


        return monthDiff;
    }




    public static int DayDiff(int day){
        int dayDiff = 0;
       // 之前的月差,缺一个+6,-day
        dayDiff += 6;
        dayDiff -= day;



        return dayDiff;
    }



  // 此方法用于返回年/月的天数
    public static int getMonthDays(int year,int month){
        int monthDays = 0;

        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                monthDays = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                monthDays = 30;
                break;
            case 2:
                monthDays = ((year%4==0&&year%100!=0)||year%400==0)?29:28;
                break;
        }


        return monthDays;
    }




    // 我先编一个,日期进来就变成天数
    // 进来的字符串
    public static int GetDaysByYYMMDD(String ss){
        int sum = 0;
        int year = 0;
        int month = 0;
        int day = 0;
        int count = 0;
        int index = 0;
        // 进来先解析字符串
        for (int i = 0; i <= ss.length()-1; i++) {
            if (count<4&&ss.charAt(i) >= '0' && ss.charAt(i) <= '9') {
                year += (ss.charAt(i)-'0')*Math.pow(10,(3-count));
                count++;
            }else if (count >= 4){
                count=0;
                index = i;
                break;
            }
        }

        for (int i = index; i <= ss.length()-1; i++) {
            if (count<2) {
                if (ss.charAt(i) >= '0' && ss.charAt(i) <= '9'){
                    month += (ss.charAt(i)-'0')*Math.pow(10,(1-count));
                    count++;
                }else if (count == 1&&(!(ss.charAt(i) >= '0' && ss.charAt(i) <= '9'))){
                    month = month/10;
                    count=3;
                }

            }else if (count >= 2){
                count=0;
                index = i;
                break;
            }
        }

        for (int i = index; i <= ss.length()-1; i++) {
            if (count<2) {
                if (ss.charAt(i) >= '0' && ss.charAt(i) <= '9'){
                    day += (ss.charAt(i)-'0')*Math.pow(10,(1-count));
                    count++;
                }else if (count == 1&&(!(ss.charAt(i) >= '0' && ss.charAt(i) <= '9'))){
                    day = day/10;
                    count=3;
                }else if (count >= 2){
                    count=0;
                    break;
                }

            }
        }

        if (count==1){
            day = day/10;
        }


//        System.out.println(year+":"+month+":"+day);
        sum = YearDiff(year)+MonthDiff(year, month)+DayDiff(day);
        if (sum>=73050){
            sum = -999;
        }

        return sum;
    }
}
